home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Beziers and Other Splines / Bezier / Bezier.cs next >
Encoding:
Text File  |  2001-01-15  |  2.0 KB  |  77 lines

  1. //-------------------------------------
  2. // Bezier.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class Bezier: Form
  9. {
  10.      protected Point[] apt = new Point[4];
  11.  
  12.      public static void Main()
  13.      {
  14.           Application.Run(new Bezier());
  15.      }
  16.      public Bezier()
  17.      {
  18.           Text = "Bezier (Mouse Defines Control Points)";
  19.           BackColor = SystemColors.Window;
  20.           ForeColor = SystemColors.WindowText;
  21.           ResizeRedraw = true;
  22.  
  23.           OnResize(EventArgs.Empty);
  24.      }
  25.      protected override void OnResize(EventArgs ea)
  26.      {
  27.           base.OnResize(ea);
  28.  
  29.           int cx = ClientSize.Width;
  30.           int cy = ClientSize.Height;
  31.  
  32.           apt[0] = new Point(    cx / 4,     cy / 2);
  33.           apt[1] = new Point(    cx / 2,     cy / 4);
  34.           apt[2] = new Point(    cx / 2, 3 * cy / 4);
  35.           apt[3] = new Point(3 * cx / 4,     cy / 2);
  36.      }
  37.      protected override void OnMouseDown(MouseEventArgs mea)
  38.      {
  39.           Point pt;
  40.  
  41.           if (mea.Button == MouseButtons.Left)
  42.                pt = apt[1];
  43.  
  44.           else if (mea.Button == MouseButtons.Right)
  45.                pt = apt[2];
  46.  
  47.           else
  48.                return;
  49.  
  50.           Cursor.Position = PointToScreen(pt);
  51.      }
  52.      protected override void OnMouseMove(MouseEventArgs mea)
  53.      {
  54.           if (mea.Button == MouseButtons.Left)
  55.           {
  56.                apt[1] = new Point(mea.X, mea.Y);
  57.                Invalidate();
  58.           }
  59.           else if (mea.Button == MouseButtons.Right)
  60.           {
  61.                apt[2] = new Point(mea.X, mea.Y);
  62.                Invalidate();
  63.           }
  64.      }
  65.      protected override void OnPaint(PaintEventArgs pea)
  66.      {
  67.           Graphics grfx = pea.Graphics;
  68.  
  69.           grfx.DrawBeziers(new Pen(ForeColor), apt);
  70.  
  71.           Pen pen = new Pen(Color.FromArgb(0x80, ForeColor));
  72.  
  73.           grfx.DrawLine(pen, apt[0], apt[1]);
  74.           grfx.DrawLine(pen, apt[2], apt[3]);
  75.      }
  76. }
  77.